package com.heyqing.topic.MaxProfit_0121;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * ClassName:MaxProfit
 * Package:com.heyqing.topic.MaxProfit_0121
 * Description:
 *
 * @Date:2024/9/15
 * @Author:Heyqing
 */
public class MaxProfit {
    public int maxProfit(int[] prices) {
        int minBuyIn = 10000;
        int maxBuyOut = -1;
        int buyInIndex = 0;
        int buyOutIndex = 0;
        Set<Integer> res = new HashSet<>();
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minBuyIn){
                minBuyIn = prices[i];
                buyInIndex = i;
            }
            if (prices[i] > maxBuyOut){
                maxBuyOut = prices[i];
                buyOutIndex = i;
            }
            if (buyInIndex > buyOutIndex){
                maxBuyOut = prices[buyInIndex];
                buyOutIndex = buyInIndex;
            }else{
                res.add(prices[buyOutIndex] - prices[buyInIndex]);
            }
        }
        return Collections.max(res);
    }
}
